package com.huika.renrench.control.login.activity;

import android.content.Intent;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.Response.Listener;
import com.google.gson.reflect.TypeToken;
import com.huika.lib.validation.EditTextValidator;
import com.huika.lib.validation.ValidationModel;
import com.huika.renrench.GlobalApp;
import com.huika.renrench.R;
import com.huika.renrench.config.Constant;
import com.huika.renrench.config.UrlConstants;
import com.huika.renrench.control.common.base.BaseAct;
import com.huika.renrench.control.login.event.LoginEvent;
import com.huika.renrench.control.login.validations.PhoneNumberValidation;
import com.huika.renrench.control.login.validations.VerifyNumberValidation;
import com.huika.renrench.http.FormResultRequest;
import com.huika.renrench.http.JSONWrapAjaxParams;
import com.huika.renrench.http.RequestResult;
import com.huika.renrench.support.bean.FindPwdCodeBean;
import com.huika.renrench.support.bean.FindPwdVerfyBean;
import com.huika.renrench.view.TitleBarHelper;
import com.huika.renrench.view.clearedit.ClearableEditText;

import de.greenrobot.event.EventBus;

public class FindPwdActivity extends BaseAct {
	private ClearableEditText etPhone;
	private ClearableEditText etVerfy;
	private TextView btnVerfy;
	private Button btnNext;

	private EditTextValidator phoneValidator;
	private EditTextValidator editTextValidator;

	private String phone;

	@Override
	public void setRootView() {
		setContentView(R.layout.activity_find_pwd);
		EventBus.getDefault().register(this);
	}
	
	public void onEventMainThread(LoginEvent loginEvent) {
		if (loginEvent.getLoginStatus() == 1) {
			this.finish();
		}
	}

	@Override
	protected void onDestroy() {
		EventBus.getDefault().unregister(this);
		super.onDestroy();
	}

	@Override
	protected void initWidget() {
		TitleBarHelper titleBarHelper = new TitleBarHelper(this, -1, -1, R.string.title_findpwd);
		titleBarHelper.setLeftMsg("登录");
		etPhone = (ClearableEditText) findViewById(R.id.et_phone_afp);
		etVerfy = (ClearableEditText) findViewById(R.id.et_verfy_afp);
		btnVerfy = (TextView) findViewById(R.id.btn_verfy_afp);
		btnNext = (Button) findViewById(R.id.btn_next_afp);
		setVerfyEt(false);
		initLiseter();
		phoneValidator = new EditTextValidator(this).add(new ValidationModel(etPhone, new PhoneNumberValidation())).execute();
		editTextValidator = new EditTextValidator(this).add(new ValidationModel(etVerfy, new VerifyNumberValidation())).execute();
		etPhone.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (s.length() > 0) {
					if (!s.toString().substring(0, 1).equals("1")) {
						etPhone.setText("");
					} else if (s.length() > 1 && !s.toString().substring(1, 2).matches("[345789]")) {
						etPhone.setText("1");
						etPhone.setSelection(etPhone.length());
					}
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
			}
		});
	}

	private void initLiseter() {
		btnVerfy.setOnClickListener(this);
		btnNext.setOnClickListener(this);
	}

	@Override
	public void widgetClick(View v) {
		switch (v.getId()) {
		case R.id.btn_verfy_afp:
			verfy();
			break;
		case R.id.btn_next_afp:
			next();
			break;

		default:
			break;
		}
	}

	private void verfy() {
		if (phoneValidator.validate()) {
			showLoadingDialog("");
			phone = getInputStr(etPhone);
			FormResultRequest<FindPwdCodeBean> request = new FormResultRequest<FindPwdCodeBean>(UrlConstants.GETFINDPASSWORDSMSCODE, codeListener, this, new TypeToken<RequestResult<FindPwdCodeBean>>() {
			}.getType());
			JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
			ajaxParams.putStringTypeParam("phone", phone);
			ajaxParams.putStringTypeParam("ip", GlobalApp.ip);
			ajaxParams.putStringTypeParam("mac", GlobalApp.mac);
			ajaxParams.putStringTypeParam("dev", Constant.DEV);
			request.setRequestParams(ajaxParams);
			executeRequest(request);
		}
	}

	private void next() {
		if (phoneValidator.validate() && editTextValidator.validate()) {
			showLoadingDialog("");
			FormResultRequest<FindPwdVerfyBean> request = new FormResultRequest<FindPwdVerfyBean>(UrlConstants.FINDPASSWORDCHECKCODE, verfyListener, this,
					new TypeToken<RequestResult<FindPwdVerfyBean>>() {
					}.getType());
			JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
			ajaxParams.putStringTypeParam("phone", phone);
			ajaxParams.putStringTypeParam("validateCode", getInputStr(etVerfy));
			ajaxParams.putStringTypeParam("ip", GlobalApp.ip);
			ajaxParams.putStringTypeParam("mac", GlobalApp.mac);
			ajaxParams.putStringTypeParam("dev", Constant.DEV);
			request.setRequestParams(ajaxParams);
			executeRequest(request);
		}
	}

	private Listener<RequestResult<FindPwdCodeBean>> codeListener = new Listener<RequestResult<FindPwdCodeBean>>() {
		@Override
		public void onResponse(RequestResult<FindPwdCodeBean> arg0) {
			dismissLoadingDialog();
			showToastMsg(arg0.msg);
			btnVerfy.setClickable(false);
			setVerfyEt(true);
			new CountDownTimer(120000, 1000) {
				public void onTick(long millisUntilFinished) {
					btnVerfy.setTextColor(getResources().getColor(R.color.color_969696));
					btnVerfy.setText(String.format("%s秒", String.valueOf(millisUntilFinished / 1000)));
				}

				public void onFinish() {
					btnVerfy.setTextColor(getResources().getColor(R.color.color_f25522));
					btnVerfy.setText("获取验证码");
					btnVerfy.setClickable(true);
				}
			}.start();
		}
	};

	private Listener<RequestResult<FindPwdVerfyBean>> verfyListener = new Listener<RequestResult<FindPwdVerfyBean>>() {

		@Override
		public void onResponse(RequestResult<FindPwdVerfyBean> arg0) {
			dismissLoadingDialog();
			if (arg0.flag == 1) {
				FindPwdVerfyBean bean = arg0.getRs();
				Intent intent = new Intent(FindPwdActivity.this, SetNewPwdActivity.class);
				intent.putExtra("FindPwdVerfyBean", bean);
				intent.putExtra("phone", phone);
				showActivity(aty, intent);
			}else{
				showToastMsg(arg0.msg);
			}
		}
	};
	
	private void setVerfyEt(boolean isShow) {
		if (isShow) {
			etVerfy.setEnabled(true);
		} else {
			etVerfy.setEnabled(false);
		}
	}

}
